package pers.cocoadel.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.cocoadel.springcloud.exception.BlockExHandler;
import pers.cocoadel.springcloud.service.PaymentSerivce;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController
{
    @Autowired
    PaymentSerivce paymentSerivce;

    @GetMapping("/payment/testA")
    @SentinelResource(value = "payment/testA",blockHandlerClass = BlockExHandler.class,blockHandler = "defaultHandler")
    public String testA()
    {
        return "test A";
    }

    @GetMapping("/payment/testB")
    public String testB()
    {
//        try
//        {
//            TimeUnit.SECONDS.sleep(1);
//        }
//        catch (InterruptedException e)
//        {
//            e.printStackTrace();
//        }
//        int i = 10 / 0;
//        System.out.println(Thread.currentThread().getName() + " testB");
        return "test B";
    }

    @GetMapping("payment/testLink1")
    @SentinelResource(value = "payment/testLink1")
    public String testLink1()
    {
        return "link1------" + paymentSerivce.testLink();
    }

    @GetMapping("payment/testLink2")
    @SentinelResource(value = "payment/testLink2")
    public String testLink2()
    {
        return "link2------" + paymentSerivce.testLink();
    }


    @GetMapping("/payment/hotKey")
    @SentinelResource(value = "payment/hotKey", blockHandlerClass = BlockExHandler.class, blockHandler = "hotKeyHandler")
//    @SentinelResource(value = "payment/hotKey",blockHandler = "hotKeyHandler")
    public String testHotKey(@RequestParam(name = "p1", required = false) String p1,
                             @RequestParam(name = "p2", required = false) String p2)
    {
        return "hotKey Test";
    }

    public String hotKeyHandler(String p1, String p2, BlockException ex)
    {
        return "服务器繁忙！/(ㄒoㄒ)/~~";
    }


}
